
$(function () {
    var focus = {
        list: $('.list'),
        init: function () {
            this.event();
        },
        event: function () {
            this.getfocus();
        },
        // 获取收藏列表
        getfocus: function () {
            var self = this , ajax = new AjaxHelper();
            ajax.get('/user/focus/list', '', function (data) {
                console.log(data)
                // 判断是否有值
                if (data.results.length <= 0) {
                    self.list.append('<p class="noData">没有数据， 快去收藏吧 ~ </p>'); 
                } else {
                    $.each(data.results, function (i, item) {
                        var t = simpleTpl();
                        t._('<div class="list-item img-txt-list-item">')
                            ._('<a href="/user/'+item.touser._id+'/article" class="focus-people-container">')
                                ._('<div class="pull-left img-item">')
                                    ._('<img src="'+item.touser.headimgurl+'">')
                                ._('</div>')
                                ._('<div class="pull-right txt-item">')
                                    ._('<p class="item-title">'+item.touser.username+'</p>')
                                    ._('<div class="cancel-focus" data-touserId='+item.touser._id+'>取消关注</div>')
                                ._('</div>')
                            ._('</a>')
                        t._('</div>');

                        self.list.attr({
                            'data-pageNumber': data.pageNumber,
                            'data-pageCount': data.pageCount
                        });
                        self.list.append(t.toString());
                        $.hideLoading();
                    });
                }
            });
        },
        cancelFocus: function () {
            $('body').on('click', '.cancel-focus', function () {
                var touserId = $(this).attr('data-touserId');
                if (touserId) {
                     ajax.get('/user/'+touserId+'/focus/del', '', function (data) {
                        console.log(data)
                        // 成功
                        if (data.results == 1) {
                            location.reload();
                        } 
                        // 失败
                        else {
                            $.toptips('取消关注失败，请联系管理员！')
                        }
                        $.hideLoading();
                    });                   
                }
            });
        }
    };
    focus.init();    
})